var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AutoNodePool = undefined;
var $1$yyComponent = require("yyComponent");
var i = cc__importDefault($1$yyComponent);
var exp_AutoNodePool = function () {
  function _ctor(t, e) {
    this.prefab = t;
    this.scripteName = e;
    this.pool = new cc.NodePool(e);
  }
  _ctor.prototype.get = function (t) {
    // 从对象池中获取对象，如果没有则实例化一个新的对象
    let pooledObject = this.pool.get(t);
    if (!pooledObject) {
      pooledObject = cc.instantiate(this.prefab);
    }

    // 初始化组件
    let scriptComponent = null;

    // 如果有指定的脚本名称，尝试获取相应的组件
    if (this.scripteName) {
      scriptComponent = pooledObject.getComponent(this.scripteName);
    }

    // 获取默认组件
    const defaultComponent = pooledObject.getComponent(i.default);

    // 如果存在默认组件并且它有初始化方法，则调用初始化
    if (defaultComponent && defaultComponent.init) {
      defaultComponent.init(t);
    } else {
      // 如果没有有效的默认组件，清空脚本名称
      this.scripteName = null;
    }

    // 返回获取到的对象
    return pooledObject;

  };
  _ctor.prototype.put = function (t) {
    this.pool.put(t);
  };
  _ctor.prototype.clear = function () {
    this.pool.clear();
  };
  Object.defineProperty(_ctor.prototype, "count", {
    get() {
      return this.pool.size();
    },
    enumerable: false,
    configurable: true
  });
  _ctor.prototype.preCreate = function (t) {
    // 计算需要实例化的对象数量
    const neededCount = t - this.count;

    // 确保需要实例化的数量大于零
    if (neededCount > 0) {
      for (let i = 0; i < neededCount; ++i) {
        const newObject = cc.instantiate(this.prefab); // 实例化新的对象

        // 如果有指定的脚本名称，初始化相应的组件
        if (this.scripteName) {
          const scriptComponent = newObject.getComponent(this.scripteName);
          if (scriptComponent) {
            scriptComponent.init(); // 调用初始化方法
          } else {
            this.scripteName = null; // 清除脚本名称
          }
        }

        this.put(newObject); // 将对象放入池中
      }
    }

  };
  return _ctor;
}();
exports.AutoNodePool = exp_AutoNodePool;